package com.controller.lx;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.entity.User;
import com.service.ServiceException;
import com.service.lx.UserService;
import com.utils.RoleFunctionItem;
import com.utils.Result;

@Controller
public class UserController {
	@Autowired
	private UserService userService;

	/**
	 * 查询用户
	 * @return
	 */
	@PostMapping("/user_select_")
	@ResponseBody
	public Page<User> findAll(@RequestParam("field") String field, 
			@RequestParam("curpage") Integer curpage, 
			@RequestParam("pagesize") Integer pagesize){
		System.out.println(field);
		System.out.println(curpage);
		System.out.println(curpage);
		return userService.findUsersBy(field, curpage, pagesize);
	}
	
	/**
	 * 逻辑删除
	 * @param Id
	 * @throws ServiceException
	 */
	@DeleteMapping("/user_ifdelete_")
	@ResponseBody
	public Result UpdateIfdelete(Long Id){
		userService.UpdateUserIfdelete(Id);
		return Result.SUCCESS;
	}

	/**
	 * 批量删除
	 * @param qaIds
	 */
	@PostMapping("/user_delete_arr_")
	@ResponseBody
	public Result deletBatchRoles(@RequestBody JSONArray Ids) {
		userService.deletArrIfDelete(JSONObject.parseArray(Ids.toString(), Long.class));
		return Result.SUCCESS;
	}

	/**
	 * 添加/修改
	 * @param serve
	 * @throws ServiceException
	 */
	@PostMapping("/user_addandupdate_")
	@ResponseBody
	public Result addAndUpdate(@RequestBody User user){
		userService.addAndUpdateUser(user);
		return Result.SUCCESS;
	}
	

	/**
	 * 获取角色列表
	 * @param userId
	 * @return
	 */
	@GetMapping("/user_getroles_")
	@ResponseBody
	public Map getFunctionsByRole(Long userId) {
		List<RoleFunctionItem> all = userService.queryAllRoles();
		List<Long> special = userService.getRoleByUser(userId);
		Map map = new HashMap<>();
		map.put("all", all);
		map.put("special", special);
		return map;
	}
	
	/**
	 * 给角色授权
	 * @param map
	 */
	@PostMapping("/user_addroles_")
	@ResponseBody
	public Result grantForRole(@RequestBody Map map) {
		try {
			userService.grantForRole(map);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.FAILURE("授权失败！");
		}
	}
	
	/**
	 * 用户登陆
	 * @param name
	 * @param password
	 * @return
	 */
	@PostMapping("/login")
	@ResponseBody
	public Map login(@RequestBody User u, HttpSession session) {
		System.out.println(u.getUserUid());
		System.out.println(u.getUserPwd());
		User user = userService.login(u.getUserUid(), u.getUserPwd());
		Map map = new HashMap();
		if(user != null) {
			session.setAttribute("user", user);
			map.put("success", true);
			map.put("user", user);
			map.put("functions", userService.getFunctionsByUserId(user.getUserId()));
		}else {
			map.put("success", false);
		}
		return map;
	}
	
	/**
	 * 查询用户名
	 * @return
	 */
	@PostMapping("/user_select_uid")
	@ResponseBody
	public User findUid(@RequestParam("userUid") String userUid){
		return userService.findUsersByUid(userUid);
	}
}
